package cn.edu.hqu.javaee.web;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;


import cn.edu.hqu.javaee.domain.Choose;
import cn.edu.hqu.javaee.domain.Course;
import cn.edu.hqu.javaee.domain.Course.Type;
import cn.edu.hqu.javaee.domain.Personal;
import cn.edu.hqu.javaee.repository.ChooseRepository;
import cn.edu.hqu.javaee.repository.CourseRepository;


@Controller
@RequestMapping("/choose")
@SessionAttributes("personal")//只能作用在类上，作用是将指定的Model中的键值对添加至session中，方便在下一次请求中使用。
public class ChooseController {
	private final CourseRepository courseRepo;
	  private ChooseRepository chooseRepo;
	  @Autowired
	  public ChooseController(CourseRepository courseRepo,ChooseRepository chooseRepo) {
	    this.courseRepo = courseRepo;
	    this.chooseRepo = chooseRepo;
	  }

	  @ModelAttribute(name = "personal")
	  public Personal personal() {
	    return new Personal();
	  }
	  
	  @ModelAttribute(name = "choose")
	  public Choose choose() {
	    return new Choose();
	  }

	  @GetMapping
	  public String showChoose(Model model) {
	    List<Course> courses = new ArrayList<>();
	    courseRepo.findAll().forEach(i -> courses.add(i));
	    
	    Type[] types = Course.Type.values();
	    for (Type type : types) {
	      System.out.println(type.toString().toLowerCase());
	      System.out.println(filterByType(courses, type));
	      model.addAttribute(type.toString().toLowerCase(), 
	          filterByType(courses, type));      
	    }

	    return "choose";
	  }

	  @PostMapping
	  public String processChoose(@Valid Choose choose, Errors errors, @ModelAttribute Personal personal,HttpServletRequest request,HttpServletResponse response) throws Exception {
		  if (errors.hasErrors()) {
			return "choose";
		}
		formSubmit(request,response);
	    Choose saved = chooseRepo.save(choose);
	    personal.addDesign(saved);
	    System.out.println("choose"+choose);
	    System.out.println("personal"+personal);
	    return "redirect:/checkout/personal";
	  }

	  
	 

	private List<Course> filterByType(
	      List<Course> courses, Type type) {
	    return courses
	              .stream()
	              .filter(x -> x.getType().equals(type))
	              .collect(Collectors.toList());
	  }
	
	protected void formSubmit(HttpServletRequest request, HttpServletResponse response)throws Exception {
	 response.setCharacterEncoding("UTF=8");
        response.setContentType("text/html;charset=UTF-8");
        //使用request对象的getSession()获取session，如果session不存在则创建一个
        HttpSession session = request.getSession();
        String[] values=request.getParameterValues("courses");
        session.setAttribute("data",values);
    }
}
